package com.bwie.controller;


import com.baidu.aip.ocr.AipOcr;
import com.bwie.utils.R;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;


import com.bwie.pojo.TbOrders;
import com.bwie.pojo.vo.GrabOrderVo;
import com.bwie.pojo.vo.UserVo;
import com.bwie.service.TbOrdersService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


@RestController
@RequestMapping("/online/index")
public class TestController {


    @Autowired
    private TbOrdersService ordersService;

    /**
     * OCR身份证识别
     *
     * @param file
     * @return
     */
    @PostMapping("/ocrIdCard")
    public R ocrIdCard(
            @RequestPart(name = "file") MultipartFile file
    ) throws IOException {

        String APP_ID = "119873631";
        String API_KEY = "QKviFLV577uSxpbgnVY18bm5";
        String SECRET_KEY = "bbVxmZxwumGII59pjtEsVvUXrliyjEJp";
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        byte[] bytes = file.getBytes();
        JSONObject idcard = client.idcard(bytes, "front", new HashMap<>());
        JSONObject wordsResult = idcard.getJSONObject("words_result");

        JSONObject jsonObject = wordsResult.getJSONObject("姓名");
        JSONObject address = wordsResult.optJSONObject("住址");
        JSONObject sex = wordsResult.optJSONObject("性别");


        String words = jsonObject.getString("words");
        String words1 = address.getString("words");
        String words2 = sex.getString("words");

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", words);
        map.put("address", words1);
        map.put("sex", words2);

        return R.OK(map);
    }


    /**
     * 用户下单下单接口
     *
     * @return
     */
    @PostMapping("/downOrder")
    public R downOrder(
            @RequestBody TbOrders orders
    ) {
        return ordersService.downOrder(orders);

    }

    /**
     * 用户信息保存到redisGEO
     *
     * @param userVo
     */
    @PostMapping("/userToRedisGEO")
    public void userToRedisGEO(
            @RequestBody @Validated UserVo userVo
    ) {
        ordersService.userToRedisGEO(userVo);
    }

    /**
     * 查找指定坐标附近的司机
     *
     * @param longitude
     * @param latitude
     * @param radiusKm
     * @param count
     * @return
     */
    @PostMapping("/distance")
    public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> nearbyDriver(
            double longitude, double latitude, double radiusKm, long count) {
        return ordersService.nearbyDriver(longitude, latitude, radiusKm, count);
    }


    /**
     * 根据经纬度查询两地之间的直线距离
     *
     * @param placeId1
     * @param placeId2
     * @return
     */
    @GetMapping("/distance")
    public R distance(
            @RequestParam String placeId1,
            @RequestParam String placeId2,
            @RequestParam String unit

    ) {
        return ordersService.distance(placeId1, placeId2, unit);
    }

    @PostMapping("/grabOrder")
    public R grabOrder(
            @RequestBody GrabOrderVo grabOrderVo
    ) {
        boolean success = ordersService.driverGrabOrder(grabOrderVo);
        if (success) {
            return R.OK("抢单成功");
        } else {
            return R.ERROR(500, "抢单失败，订单可能已被抢走");
        }
    }

}
